package ai.esc.speedy.admin.user.infrastructure.shiro;

import ai.esc.speedy.admin.user.api.Passport;
import ai.esc.speedy.admin.user.infrastructure.jwt.JwtToken;
import ai.esc.speedy.admin.user.infrastructure.jwt.JwtUserInfo;
import ai.esc.speedy.admin.user.infrastructure.jwt.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Rubin
 * @version v1 2020/2/27 16:17
 */
@Component
public class PassportRealm extends AuthorizingRealm {

    @Autowired
    private JwtUtil jwtUtil;


    @Override
    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 认证 ，用户名密码校验
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String token = (String) authenticationToken.getCredentials();

        System.out.println("\n" + token + "\n");
        // 解密获得username，用于和数据库进行对比
        JwtUserInfo userInfo = jwtUtil.getJwtUserInfo(token);
        if (userInfo == null) {
            throw new AuthenticationException("token无效");
        }

//        SysUser userBean = sysUserService.findByUserName(username);
//        if (userBean == null) {
//            throw new AuthenticationException("用户不存在!");
//        }

//        if (!jwtUtil.verify(token, username, "2go")) {
//            throw new AuthenticationException("用户名或密码错误");
//        }

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
                "123", //用户名
                "2go", //密码
                getName() //realm name
        );

        // return new SimpleAuthenticationInfo(token, token, "my_realm");
        return authenticationInfo;


//
//
//        String username = (String) authenticationToken.getPrincipal();
////        JuuPassport user = userDao.getByUsername(username);
//
//        JuuPassport user = new JuuPassport();
//
//        if (user == null) {
//            throw new UnknownAccountException(); // 账号不存在
//        }
//        if (user.getStatus() != 0) {
//            throw new LockedAccountException();  // 账号被锁定
//        }
//        String salt = user.getSalt();
//        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,
//                user.getSaltPassword(),
//                ByteSource.Util.bytes(salt),
//                getName());
//
//        return authenticationInfo;
    }


    /**
     * 权限验证
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        Passport user = (Passport) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        // 角色
        Set<String> roles = new HashSet<>();
        // 权限
        Set<String> permissions = new HashSet<>();

        // 测试用权限
//        if ("admin".equals(user.getUsername())) {
//            roles.add("admin");
//            permissions.add("op:write");
//        } else {
//            roles.add("user");
//            permissions.add("op:read");
//        }
        authorizationInfo.setRoles(roles);
        authorizationInfo.setStringPermissions(permissions);
        return authorizationInfo;
    }

}
